/**
 * file for index.php
 */

function get_new_token() {
	dojo.byId("plugin_token").innerHTML = "<img src=\"../images/loading_small.gif\">";
	dijit.byId("new_token").set("disabled",true);
	
	var xhrArgs = {
	        url: "ajax/ajax_index.php",
	        content : {
	        	"type" : "new_token"
	    		},
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		// apply new key
					dojo.byId("plugin_token").innerHTML = jsonData.result;
					dijit.byId("new_token").set("disabled",false);
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
	
}

function galaxytool_delete_formatter(val, rowIdx, cell) {
	var id = shoutbox_store.getValues(shoutbox_grid.getItem(rowIdx),"id");
	var val = "<img src=\"../images/trash.png\" onclick=\"delete_comment("+id+")\" style=\"cursor: pointer;\" height=\"16\" width=\"16\">";
	return val;
}

function delete_comment(id) {
	id = parseInt(id);
	if (id < 1) return;
	
	var xhrArgs = {
	        url: "ajax/ajax_shoutbox.php",
	        content : {
	        	"type" : "delete",
				"id" : id
	    		},
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		galaxytool_reload_shoutbox();
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);

}

function add_comment() {
	var comment = dijit.byId("shouttext").get("value");
	if (comment == "") return;
	
	var xhrArgs = {
	        url: "ajax/ajax_shoutbox.php",
	        content : {
	        	"type" : "insert",
				"comment" : comment
	    		},
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		galaxytool_reload_shoutbox();
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
}

function galaxytool_reload_shoutbox() {
	// request reload store content and re-create grid
	shoutbox_store.close();
	shoutbox_store = new dojo.data.ItemFileReadStore({url:"ajax/ajax_shoutbox.php?type=read"});
	grid_layout_done = false;
	shoutbox_grid.setStore(shoutbox_store);
	dijit.byId("shouttext").set("value","");
}

function galaxytool_add_comment(e) {
	if (e != null && e.keyCode != 13) {
		return; // ignore onKeyPress event if other keys than ENTER are hit
	}
	// submit form
	add_comment();
}

function galaxytool_index_init() {
	dojo.connect(dijit.byId("shouttext"), "onKeyPress", galaxytool_add_comment );
}

dojo.ready(galaxytool_index_init);